﻿using System;
using System.Windows.Controls;
using LaserTag.Infrastructure.Events;
using LaserTag.Module.MediaKit.ViewModels;
using Microsoft.Practices.Composite;
using Microsoft.Practices.Composite.Events;
using WPFMediaKit.DirectShow.MediaPlayers;

namespace LaserTag.Module.MediaKit.Views
{
    public partial class VideoCaptureView : UserControl, IVideoCaptureView, IActiveAware
    {
        private readonly IEventAggregator m_eventAggregator;

        public VideoCaptureView()
        {
            
        }

        public VideoCaptureView(IEventAggregator eventAggregator)
        {
            m_eventAggregator = eventAggregator;

            InitializeComponent();
        }

        #region IVideoCaptureView Members

        public void SetViewModel(IVideoCaptureViewModel viewModel)
        {
            DataContext = viewModel;
        }

        #endregion

        private void VideoCaptureElement_NewVideoSample(object sender, VideoSampleArgs e)
        {
            m_eventAggregator.GetEvent<NewVideoFrameEvent>().Publish(e.VideoFrame);
        }

        #region IActiveAware Members

        private bool m_isActive;

        public bool IsActive
        {
            get { return m_isActive; }
            set
            {
                m_isActive = value;
                InvokeIsActiveChanged(null);
                videoElement.IsRenderingEnabled = m_isActive;
            }
        }

        public event System.EventHandler IsActiveChanged;

        private void InvokeIsActiveChanged(EventArgs e)
        {
            EventHandler changed = IsActiveChanged;
            if (changed != null) changed(this, e);
        }

        #endregion
    }
}
